package components
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.controls.Button;
	import mx.controls.ComboBox;
	import mx.controls.Label;
	import mx.controls.RichTextEditor;
	import mx.controls.TextInput;
	import mx.events.ResizeEvent;
	
	import nl.wv.extenders.panel.SuperPanel;
	
	import tools.*;
	
	public class NewTopic{
		private var panel:SuperPanel = new SuperPanel();
		private var newTopicBtn:Button = new Button();
		private var tekstEditor:RichTextEditor = new RichTextEditor();
		private var naslov:TextInput = new TextInput();
		private var service:Mysql = new Mysql();
		private var tool:Tool = new Tool();
		private var materijali:ComboBox = new ComboBox();
		private var labela:Label = new Label();
		
		private var materijal:XML;
		private var username:String;
		private var kurs:String;
		private var naziv:String;
		
		public function NewTopic(p:SuperPanel){
			panel = p;
			panel.addEventListener(ResizeEvent.RESIZE, onResize);
			newTopicBtn.label = "DODAJ TEMU";
			newTopicBtn.x = panel.width-119;
			newTopicBtn.y = panel.height-45;
			labela.x=10;
			labela.y=25;
			materijali.x = 10;
			materijali.y = panel.height-45;
			materijali.height = 20;
			materijali.width = panel.width-150;
			newTopicBtn.addEventListener(MouseEvent.CLICK, onNewTopicBtnClick);
			naslov.x = 10;
			naslov.y = 40;
	 		naslov.height=20;
	 		naslov.width=panel.width-30;
	 		naslov.toolTip = "Unesite naslov teme";
			tekstEditor.x = 10;
			tekstEditor.y = 60;
	 		tekstEditor.height=panel.height-111;
	 		tekstEditor.width=panel.width-30;
			tekstEditor.title="Unesite tekst teme: ";
			p.addChild(newTopicBtn);
			p.addChild(tekstEditor);
			p.addChild(naslov);
			p.addChild(materijali);
			p.addChild(labela);
			tekstEditor.colorPicker.text="Boja";
		}
		
		public function setParameters(m:XML, u:String, k:String, n:String):void{
			trace(m);
			materijal = m;
			username = u;
			kurs = k;
			naziv = n;
			var pom:XML= new XML(new String("<materijal id='0' naziv='Ova tema nije vezana za materijal' />")); 
			var xmlList:XMLList = pom+materijal.children();
			materijali.dataProvider = xmlList;
			materijali.labelField = "@naziv";
			labela.htmlText = "Unesite naslov nove teme za kurs <b>\""+naziv+"\"</b>"; 
		}
		
		public function onNewTopicBtnClick(e:Event):void{
		if(naslov.text.length>0&&tekstEditor.text.length>0){
			var help:String=tekstEditor.htmlText;
			if(tool.checkForCRC(help)){
				tool.createAlert("Greska!", "Rijec 'CRC' se ne smije nalaziti u tekstu!", 200);
			} else {			
				help=tool.CodeTekst(tekstEditor.htmlText);
            	if(materijali.selectedItem.@id==0){
            		service.sql = {type:"INSERT", into:"tema", columns:"naslov, tekst, kurs, username, datum, vrijeme", values:"@"+naslov.text+"@,@"+help+"@,@"+kurs+"@,@"+username+"@,@"+tool.currentDate()+"@,@"+tool.currentTime()+"@"};
            	} else service.sql = {type:"INSERT", into:"tema", columns:"naslov, tekst, kurs, username, materijal, datum, vrijeme", values:"@"+naslov.text+"@,@"+help+"@,@"+kurs+"@,@"+username+"@,@"+materijali.selectedItem.@id+"@,@"+tool.currentDate()+"@,@"+tool.currentTime()+"@"}; 
				service.addEventListener(service.MYSQL_RESULT, onResult);
 				service.startService();
 			}
 		} else tool.createAlert("Greska!", "Morate unijeti naslov i tekst!", 200);
		}
		
		public function onResult(e:Event):void{
			if(service.xml.results.result){
				tool.createAlert("Info", "Tema je uspjesno upisana!\nOtvorite ponovo prozor sa kursevima ako se tema ne nalazi na spisku!", 200);
				panel.closePanel();
			} else tool.createAlert("Info", "Pri slanju teme dogodila se greska, pokusajte ponovo!", 200);
		}
		
		public function onResize(e:Event):void{
			newTopicBtn.x = panel.width-119;
			newTopicBtn.y = panel.height-45;
	 		naslov.width=panel.width-30;
	 		tekstEditor.height=panel.height-111;
	 		tekstEditor.width=panel.width-30;
			materijali.width = panel.width-150;
			materijali.y = panel.height-45;
		}
	}
}